package com.hxy.modules;

import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Gaospatial地理位置
 * @author YunGang.Guo
 * @date 2022/03/24 13:55
 **/
public class Gaospatial {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        System.out.println(jedis.ping());
        Long memberLong = addMember(jedis);
        System.out.println("成功添加城市：" + memberLong);
        //查询一个或多个地理位置坐标
        List<GeoCoordinate> shenzhen = jedis.geopos("china:city", "shenzhen");
        System.out.println("地理坐标为：" + shenzhen);

        //查询两地之间的距离
        Double geodist = jedis.geodist("china:city", "shenzhen", "hangzhou", GeoUnit.KM);
        System.out.println("两地距离为：" + geodist);

        //以坐标为中心来范围搜索地理位置
        List<GeoRadiusResponse> georadius = jedis.georadius("china:city", 114.08, 22.54, 1000, GeoUnit.KM);
        System.out.println("以坐标为中心在该范围内的城市：");
        for (GeoRadiusResponse geoRadiusResponse : georadius) {
            System.out.print(geoRadiusResponse.getMemberByString() + ",  ");
        }

        //以位置为中心来范围搜索地理位置
        List<GeoRadiusResponse> responseList = jedis.georadiusByMember("china:city", "shenzhen", 2000, GeoUnit.KM);
        System.out.println("\n以位置为中心在该范围内的城市：");
        for (GeoRadiusResponse geoRadiusResponse : responseList) {
            System.out.print(geoRadiusResponse.getMemberByString() + "， ");
        }

    }

    /**
     * 添加城市
     *
     * @param jedis
     */
    public static Long addMember(Jedis jedis) {
        Map<String, GeoCoordinate> memberMap = new HashMap<>();
        //深圳
        memberMap.put("shenzhen", new GeoCoordinate(114.08, 22.54));
        //北京
        memberMap.put("beijing", new GeoCoordinate(116.10, 39.90));
        //郑州
        memberMap.put("zhengzhou", new GeoCoordinate(113.66, 34.75));
        //太原
        memberMap.put("taiyuan", new GeoCoordinate(112.54, 37.85));
        //广州
        memberMap.put("guangzhou", new GeoCoordinate(113.28, 23.12));
        //昆明
        memberMap.put("kunming", new GeoCoordinate(102.71, 25.04));
        return jedis.geoadd("china:city", memberMap);
    }


}
